<?php
/*
 For Table
 --- Searching
 Get userID
*/
function getUserID($value){
    $db = new Database;
    $getName = $db->query("SELECT * FROM tbuser where firstname LIKE '%$value' OR middlename LIKE '%$value' OR lastname LIKE '%$value'","array");
    foreach($getName as $name){
        return $name['id'];
    }
}

// User Level
function userLevelAuth(){
    $db = new Database();
    $auth = Auth::getAuth('current_user');
    $level = $auth['userlevel_id'];
    $user_level = $db->query("SELECT * FROM tbuserlevel WHERE id='$level'","row");
    if($user_level['userlevel'] != "Admin"){
        header('Location: /formalistic/'); 
    }
}

// For steps
function userStepsProcedure(){
    $db = new Database();
    $auth = Auth::getAuth('current_user');
    $steps = $auth['steps'];
    if($steps=="0"){
        header('Location: /formalistic/org_chart'); 
    }
}

/* For Posting Announcement */
function announcementComment($announcementID,$userID,$userExtension,$userName,$announcement,$date){
    ?>
    <script>
        jQuery("label.timeago").timeago();  
    </script>
    <?php
    $db = new Database;
    $auth = Auth::getAuth('current_user');
    $user_id = $auth['id'];
    /* Less Text to show */
    $announcement = $announcement;
    $post = substr($announcement, 0, 200);
    $announcementLength = strlen($announcement);
    
    $return .='
        <li class="postContainerList" id="myPost_'.$announcementID.'">
            <div>';
                $return .= myAvatar($userExtension,$userID,"50","50","none");
    $return .='
            </div>
            <div>
                <div class="requestPost">
                '.$userName.'
                </div>
                <div class="requestContentPost" id="viewComment_'.$announcementID.'">';
                $return .= $post;
                    if($announcementLength>200){
                        $return .= '
                                <br /><br />
                                <div class="requestButtonPost">
                                    <a class="hrefPostAction" data-action="viewComment_" id="loadMore" data-id="'.$announcementID.'" data-announcement-length="'.$announcementLength.'" data-announcement="'.$announcement.'">
                                        View More
                                    </a>
                                </div>';
                    }
    $return .= '
                </div>
                <div class="requestButtonPost">
                <label class="hrefPostAction viewCommentBox" data-id="'.$announcementID.'">Comment</label>';
            if($user_id==$userID){
                $return .= '
                |
                <label class="hrefPostAction deletePost" data-id="'.$announcementID.'">Delete</label>';
            }
    $return .= '
                <label class="fontGray timeago pull-right" style="margin-right:15px;font-size:10px;" title="'.$date.'"></label>
                </div>
                <div class="addCommentPost" id="addComment_'.$announcementID.'_1">
                    ';
                    // Function to get your additional comment
                    $return .= addComment($announcementID,$announcementID,1,"LIMIT 4"," ").
                    '<div class="appendNewComment" id="appendNewComment_'.$announcementID.'_1"></div>';
    $return .='
                    <div class="userAvatar">';
                    //  Function to get your avatar
                        $return .= myAvatar($auth['extension'],$user_id,"30","30","none");
                        
    $return .='                    
                    </div>
                    <div class="commentPost">
                        <textarea class="input-comment" id="newComment_'.$announcementID.'_1" style="height:30px;width:350px;"></textarea><br />
                        <input type="button" class="button-icon addButtonComment" data-comment-type="1" value="Comment" data-id="'.$announcementID.'">
                    </div>
                    
                </div>
            </div>
        </li>';
    return $return;
    
}
// Function for Showing added comment to the post
function addComment($announcementID,$announcementID,$type,$limit,$more){
    $db = new Database;
    if($type!=1){
        $typeQuery = 2;
    }
    $getComment = $db->query("SELECT * FROM (
    SELECT * FROM tbcomment WHERE annoucement_id='$announcementID' and type='$typeQuery' ORDER BY id DESC $limit
) sub
ORDER BY id ASC","array");
    $count = $db->query("SELECT * FROM tbcomment WHERE annoucement_id='$announcementID' and type='$typeQuery' ORDER BY id ASC","numrows");
    if($count>4){
        $return .= '<div class="requestButtonPost cursor showMoreComment '.$more.'" id="show_'.$announcementID.'_'.$type.'" data-type="'.$type.'" data-id="'.$announcementID.'">Show ('.$count.') More</div>';
    }
    $return .= '<div class="showMoreCommentResult_'.$announcementID.'_'.$type.'">';
    foreach($getComment as $comment){
        $getcommentID = $comment['user_id'];
        $getAnnouncementID = $comment['annoucement_id'];
        $commentUser = $db->query("SELECT * FROM tbuser WHERE id='$getcommentID'","row");
        $userFname = $commentUser['firstname'] . " " . $commentUser['lastname'];
        $return .= '<div id="deleteComment_'.$comment['id'].'"><div class="userAvatar">';
                $return .= myAvatar($commentUser['extension'],$commentUser['id'],"30","30","none");
        $return .= '
            </div>';
        $return .= newComment($comment['comment'],$comment['id'],$getcommentID,$comment['date']);
        $return .= '
            </div>';    
    }
    $return .= '</div>';
    return $return;
}
// Function for New Comment
function newComment($comment,$commentID,$getcommentID,$date){
    ?>
    <script>
        jQuery("label.timeago").timeago();  
    </script>
    <?php
    $db = new Database;
    $auth = Auth::getAuth('current_user');
    $getCommentUser = $db->query("SELECT * FROM tbuser WHERE id='$getcommentID'","row");
    $userFname = $getCommentUser['firstname']. " ".$getCommentUser['lastname'];
    /* Less Text to show */
    $announcement = $comment;
    $post = substr($announcement, 0, 200);
    $announcementLength = strlen($announcement);
    
    $return .= '
        <div class="commentPost">
            <div class="userComment">
            '.$userFname.'
            </div>
            <div class="userAddComment" id="viewAddComment_'.$commentID.'">';
                $return .= $post;
            if($announcementLength>200){
                $return .= '
                        <br /><br />
                        <div class="requestButtonPost">
                            <a class="hrefPostAction" data-action="viewAddComment_" id="loadMore" data-id="'.$commentID.'" data-announcement-length="'.$announcementLength.'" data-announcement="'.$announcement.'">
                                View More
                            </a>
                        </div>';
            }
    $return .= '
            </div>
                <div class="requestButtonPost">';
    //Show Delete button if user is logged
    if($auth['id']==$getcommentID){
        $return .= '
                <label class="hrefPostAction deleteNewComment" data-id="'.$commentID.'">Delete</label>
                ';
            
    }
    $return .= '
                <label class="fontGray timeago pull-right" style="font-size:10px;" title="'.$date.'"></label>
                </div>
                
                <div class="hr"></div>
            </div>';
    return $return;
}
// Function for your Avatar
function myAvatar($userExtension,$userID,$width,$height,$style){
    if(!empty($userExtension)){
        $return .='
        <div style="float:left;">';
        if($style!="none"){
            $return .='<div style="position:absolute;background-color:lime;z-index:1000;height:5px;width:5px;"></div>';
        }
        $return .='
            <img src="/images/avatar/'.$userID.'/1/yourAvatar_'.$userID.'.'.$userExtension.'"  style="float: left;height: '.$height.'px;width: '.$width.'px;margin-top: -3px;margin-right:5px; '.$style.'">
        </div>
        ';
    }else{
        $return .='
        <div style="float:left;">';
        if($style!="none"){
            $return .='<div style="position:absolute;background-color:lime;z-index:1000;height:5px;width:5px;"></div>';
        }
        $return .='
            <img src="/images/avatar/avatar.png"  style="float: left;height: '.$height.'px;width: '.$width.'px;margin-top: -3px;margin-right:5px; '.$style.'">
        </div>
        ';
    }
    return $return;
}
?>